﻿using studentPortal.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace studentPortal.Controllers
{
    public class AccountController : Controller
    {
        IAccount ac;
        public AccountController(IAccount ac1)
        {
            this.ac = ac1;
        }

        //
        // GET: /Account/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult signin()
        {
            if(Session["username"] != null)
            {
                return Redirect("../User/ProfilePage");
            }
            return View();
        }


        //[HttpPost]
        //public ActionResult validateSignin(user u)
        //{
        //    try
        //    {
        //        user q = ac.getUserFromPwd(u.username, u.pwd);
                
        //        if (q == null)
        //        {
        //            return HttpNotFound();
        //        }

        //        Session["username"] = u.username;

        //        return Redirect("../User/ProfilePage");
        //    }
        //    catch
        //    {
        //        checkUser = "no";
        //        return Redirect("../Account/signin");
        //    }
        
        //}


        public JsonResult validateSignin(String u, String p)
        {
            try
            {
                user q = ac.getUserFromPwd(u, p);

                if (q == null)
                {
                    return this.Json(null, JsonRequestBehavior.AllowGet);
                }
                Session["username"] = u;
                return this.Json(q, JsonRequestBehavior.AllowGet);
                
            }
            catch
            {
                return this.Json(null, JsonRequestBehavior.AllowGet);
            }

        }

        public ActionResult signup()
        {
            if (Session["username"] != null)
            {
                return Redirect("../User/ProfilePage");
            }
            return View();
        }


        public JsonResult Availability(String u)
        {
            try
            {
                var q = ac.getUser(u);

                if (q != null)
                    return this.Json(true, JsonRequestBehavior.AllowGet);
                else
                    return this.Json(false, JsonRequestBehavior.AllowGet);
            }
            catch
            {
                return this.Json(false, JsonRequestBehavior.AllowGet);
            }
        }


        [HttpPost]
        public ActionResult validateSignup(user u)
        {
            //if (ModelState.IsValid)
            //{
                HttpPostedFileBase file = Request.Files[0];
                String pathImage = @"../Images/" + file.FileName;
                file.SaveAs(Server.MapPath(pathImage));

                u.path = pathImage;
                u.joinDate = DateTime.Now.ToString();


                ac.SaveLogin(u);

                
                return Redirect("../Account/signin");
            //}
            //return Redirect("../Account/signin");
        }

        public ActionResult logout()
        {
            Session.RemoveAll();
            return Redirect("../Account/signin");
            
        }
    }
}